package ml.features;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import ml.Data;
import parser.ParserAnnotation;

public class PolarityReversalExtractor extends FeatureExtractor {

	private String[] reversals;

	public PolarityReversalExtractor(int offset) throws IOException {
		super(offset);
		BufferedReader br = new BufferedReader(new FileReader(
				"corpus/polwords/reversal.txt"));
		ArrayList<String> reversalList = new ArrayList<String>();
		while (true) {
			String line = br.readLine();
			if (line == null)
				break;
			reversalList.add(line);
		}
		br.close();
		reversals = reversalList.toArray(new String[reversalList.size()]);
	}

	@Override
	public List<Data> extract(ParserAnnotation pa, int sentence) {
		// TODO return number of polarity reversals
		throw new Error("Cannot use polarity reversal on a bag-of-features");
	}

	@Override
	public List<Data>[] extractSequence(ParserAnnotation pa, int sentence) {
		List<Data>[] datas = new List[1];
		datas[0] = new ArrayList();
		for (String ltoken : pa.getTokens(sentence)) {
			String token = ltoken.toLowerCase(Locale.ENGLISH);
			int index = Arrays.binarySearch(reversals, token);
			datas[0].add(new Data(index >= 0, offset));
		}
		return datas;
	}

	@Override
	public int nbrColumns() {
		return 1;
	}

	@Override
	public String[] wekaDescription() {
		// TODO return number of polarity reversals
		throw new Error("Cannot use polarity reversal on a bag-of-features");
	}

	@Override
	public void store() throws IOException {
	}
}
